﻿namespace fluentvaly.Tests
{
    public class UserTestObject
    {
        public string UserName { get; set; }
        public string Password { get; set; }
        public string PasswordConfirmation { get; set; }
        public bool AcceptyAGBs { get; set; }
        public Address Address { get; set; }
    }

    public class Address
    {
        public string StreetName { get; set; }
        public string HouseNumber { get; set; }
    }

    public class InvariantSetup : ValidatorConfiguration
    {
        public InvariantSetup()
        {
            UserInvariant();
            AddressInvariant();
        }

        private void UserInvariant()
        {
            var validator = DefineInvariantFor<UserTestObject>(false);

            validator.EnsureThat(u => u.UserName).IsNot(un => string.IsNullOrEmpty(un));
            validator.EnsureThat(u => u.UserName).IsNot(un => un.Length > 255);

            validator.EnsureThat(u => u.Password).Is(p => p.Length < 255);
            validator.EnsureThat(u => u.Password).IsNot(p => p.Length <= 3);
            validator.EnsureThat(u => u.Password).Is((user, password) => password == user.PasswordConfirmation);
        }

        private void AddressInvariant()
        {
            var validator = DefineInvariantFor<Address>(false);

            validator.EnsureThat(a => a.StreetName).IsNot(sn => string.IsNullOrEmpty(sn));
            validator.EnsureThat(a => a.StreetName).IsNot(sn => sn.Length > 255).If(sn => !string.IsNullOrEmpty(sn));

            validator.EnsureThat(a => a.HouseNumber).IsNot(hn => string.IsNullOrEmpty(hn));
            validator.EnsureThat(a => a.HouseNumber).IsNot(hn => hn.Length > 255).If(hn => !string.IsNullOrEmpty(hn));
        }
    }
}
